﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using TravelWallet.Types;
using WindowsPhoneRecipes;

namespace TravelWallet
{
    public partial class ProgramDetails : PhoneApplicationPage
    {
        public ProgramDetails()
        {
            InitializeComponent();
            this.BackKeyPress += new EventHandler<System.ComponentModel.CancelEventArgs>(ProgramDetails_BackKeyPress);

            if (!App.isTrialMode)
            {
                //this.ad_AdDuplex.IsEnabled = false;
                this.AdRotatorControl.IsEnabled = false;
                this.ad_upgradetofull.IsEnabled = false;

                //this.ad_AdDuplex.Visibility = System.Windows.Visibility.Collapsed;
                this.AdRotatorControl.Visibility = System.Windows.Visibility.Collapsed;
                this.ad_upgradetofull.Visibility = System.Windows.Visibility.Collapsed;

                this.p_Pivot.Height = 690;
            }
            else
            {
                //this.ad_AdDuplex.IsEnabled = true;
                this.AdRotatorControl.IsEnabled = true;
                this.ad_upgradetofull.IsEnabled = true;

                //this.ad_AdDuplex.Visibility = System.Windows.Visibility.Visible;
                this.AdRotatorControl.Visibility = System.Windows.Visibility.Visible;
                this.ad_upgradetofull.Visibility = System.Windows.Visibility.Visible;

                this.p_Pivot.Height = 590;
            }
        }

        void ProgramDetails_BackKeyPress(object sender, System.ComponentModel.CancelEventArgs e)
        {
            WalletItem item = DataContext as WalletItem;

            if (item != null)
            {
                string param = "category=" + item.Category;
                NavigationService.Navigate(new Uri("/MainPage.xaml?"+param, UriKind.Relative));
            }
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            if (NonLinearNavigationService.Instance.IsRecursiveBackNavigation == true)
            {
                return;
            }

            
            string itemId = "";

            if (NavigationContext.QueryString.TryGetValue("itemId", out itemId))
            {
                WalletItem item = App.ViewModel.GetWalletItem(Convert.ToInt64(itemId));
                DataContext = item;
                lb_Partners.DataContext = item.Program.PartnerGroup;

                if (item.Category == ProgramCategory.Hotels)
                {
                    lbl_Name.Visibility = System.Windows.Visibility.Collapsed;
                    tb_Name.Visibility = System.Windows.Visibility.Collapsed;
                }
                else
                {
                    lbl_Name.Visibility = System.Windows.Visibility.Visible;
                    tb_Name.Visibility = System.Windows.Visibility.Visible;
                }

            }
            else
            {
                throw new Exception();
            }
        }

        private void b_Edit_Click(object sender, EventArgs e)
        {
            WalletItem item = DataContext as WalletItem;

            NavigationService.Navigate(new Uri("/AddEditProgram.xaml?mode=edit&itemId="+item.ID, UriKind.Relative));
        }

        private void b_Delete_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Delete this program?", "Delete", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
            {
                WalletItem item = DataContext as WalletItem;
                App.ViewModel.DeleteItemFromWallet(item);
                NavigationService.GoBack();
            }
        }
    }
}